// 评论区的操作
function addComment() {
    // 获取博客id
    let blogId = $("#blogId").val();
    // 获取博客标题
    let blogName = $("#blogTitle").html();
    // 获取评论者名字
    let commentator = $("#commentator").val();
    // 获取评论者email，可以为空
    let email = $("#email").val();
    // 获取评论信息
    let commentBody = $("#commentBody").val();
    if (isNull(commentator)) {
        swal("请输入您的名字",{
            icon:"warning",
            button: {
                text: "好的"
            }
        })
        return;
    }
    // 匹配邮箱格式的正则表达式
    let patt = new RegExp("[\\w]+(\\.[\\w]+)*@[\\w]+(\\.[\\w])+");
    if (!isNull(email) && !patt.test(email)) {
        swal("邮箱格式不正确",{
            icon:"warning",
            button: {
                text: "好的"
            }
        })
        return;
    }
    if (isNull(commentBody)) {
        swal("请输入评论内容",{
            icon:"warning",
            button: {
                text: "好的"
            }
        })
        return;
    }

    $.ajax({
        type: "post",
        url: "/addComment",
        data: {"blogId": blogId,"blogName": blogName,"commentator": commentator,"email": email,"commentBody": commentBody},
        success: function (result) {
            if (result.resultCode == 200) {
                swal("提交评论成功",{
                    icon: "success",
                }).then(function () {
                    location.reload();
                })
            }else {
                swal(result.message,{
                    icon: "error",
                    button: {
                        text: "好的"
                    },
                })
            }
        }
    })
}

//判断变量是否为空
function isNull(obj) {
    if(obj.trim() == "" || obj == null || obj == undefined){
        return true;
    }
    return false;
}